//
//  RCUserVCCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-19.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserVCCell.h"
#import "UIImageView+WebCache.h"
#import "RCUserDataModel.h"
#import "UIImageView+WebCache.h"
#import "UIColor-Expanded.h"
#import "UIImageView+Extented.h"
#import "CommonDefines.h"
#import <QuartzCore/QuartzCore.h>
#import "NSString+NSString_Ex_.h"


@implementation RCUserVCCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


@implementation RCUserProfileCell{
    UIImageView *_userAvatar;
    UILabel     *_userNameLabel;
    UIImageView *_userSex;
    UIButton    *_userMailBox;
    UIButton    *_userPlace;
    
    //他人页面 关注/取消关注
    UIButton    *_userFollow;
    
    
    UIImageView *_indicator;
    UIButton    *_editBtn;
    
}


@synthesize delegate = _delegate;
@synthesize isMySelf = _isMySelf;

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCellPlaceHolder];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}


- (void) createCellPlaceHolder
{
    _userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 85, 70)];
    [self.contentView addSubview:_userAvatar];
    [_userAvatar release];
    
    _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 15, 130, 20)];
    [self.contentView addSubview:_userNameLabel];
    _userNameLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    _userNameLabel.textColor = kNormalOrangeTextColor;
    [_userNameLabel setBackgroundColor:[UIColor clearColor]];
    [_userNameLabel release];
    
    _userSex = [[UIImageView alloc] initWithFrame:CGRectMake(250, 15, 20, 20)];
    [self.contentView addSubview:_userSex];
    [_userSex release];
    
    _indicator = [[UIImageView alloc] initWithFrame:CGRectMake(285, 15, 20, 20)];
    [_indicator autoResizeToSetImage:[UIImage imageNamed:@"icon_RCUserVC_indicate"]];
    [self.contentView addSubview:_indicator];
    [_indicator release];
    
    _editBtn = [[UIButton alloc] initWithFrame:CGRectMake(285, 5, 40, 50)];
    [self.contentView addSubview:_editBtn];
    [_editBtn setBackgroundColor:[UIColor clearColor]];
    [_editBtn addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _editBtn.tag = 306;
    [_editBtn release];
    
    _userMailBox = [[UIButton alloc] initWithFrame:CGRectMake(110, 50, 30, 20)];
    [self.contentView addSubview:_userMailBox];
    _userMailBox.tag = 304;
    [_userMailBox setBackgroundImage:[UIImage imageNamed:@"icon_RCUserVC_message"] forState:UIControlStateNormal];
    [_userMailBox addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_userMailBox release];
    
    
    _userFollow = [[UIButton alloc] initWithFrame:CGRectMake(110, 50, 58, 25)];
    [self.contentView addSubview:_userFollow];
    _userFollow.tag = 305;
    [_userFollow setTitle:@"关注" forState:UIControlStateNormal];
    [_userFollow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_userFollow setBackgroundImage:[UIImage imageNamed:@"bg_follow_btn"] forState:UIControlStateNormal];
    [_userFollow addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_userFollow release];
    
    //二期暂时不做这个
//    _userMailBox.hidden = YES;
//    _userSex.hidden = YES;
    
    _userPlace = [UIButton buttonWithType:UIButtonTypeCustom];
    _userPlace.frame = CGRectMake(215, 50, 90, 25);
    [_userPlace setTitleColor:kNormalTextColor forState:UIControlStateNormal];
    [[_userPlace layer] setBorderWidth:1.0f];
    [[_userPlace layer] setCornerRadius:2.0f];
    [[_userPlace layer] setMasksToBounds:YES];
    [_userPlace.layer setBorderColor:[kNormalBoardColor CGColor]];
    _userPlace.enabled = NO;
    _userPlace.titleLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self.contentView addSubview:_userPlace];
    [_userPlace release];
    
    
    for (int i = 1; i<= 3; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40+107*(i-1), 90, 30, 15)];
        label.font = [UIFont fontWithName:kFontNameHelvetica size:12];
        label.textColor = kNormalTextColor;
        label.backgroundColor =[UIColor clearColor];
        if (i == 1) label.text = @"签到";
        else if (i == 2)label.text = @"点评";
        else if (i == 3)label.text = @"照片";

        UILabel *labelNumber = [[UILabel alloc] initWithFrame:CGRectMake(45+107*(i-1), 105, 30, 15)];
        labelNumber.font = [UIFont fontWithName:kFontNameHelvetica size:12];;
        labelNumber.textColor = [UIColor colorWithHex:0xff505050];
        labelNumber.backgroundColor =[UIColor clearColor];
        if (i == 1) labelNumber.tag = 201;
        else if (i == 2)labelNumber.tag = 202;
        else if (i == 3)labelNumber.tag = 203;
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(107*(i-1), 82, 107, 42)];
        btn.backgroundColor =[UIColor clearColor];
        if (i == 1) btn.tag = 301;
        else if (i == 2)btn.tag = 302;
        else if (i == 3)btn.tag = 303;
        [btn addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:label];
        [self.contentView addSubview:labelNumber];
        [self.contentView addSubview:btn];
        
        [label release];
        [labelNumber release];
        [btn release];
        
        UIImageView *verticleLine1 = [[UIImageView alloc]initWithFrame:CGRectMake(107*i, 87, 1, 35)];
        
        UIImage *img = [UIImage imageNamed:@"lv_tab_item_line"];
        verticleLine1.image = img;
//        verticleLine1.backgroundColor = [UIColor colorWithHex:0xffeaeaea];
        [self.contentView addSubview:verticleLine1];
        [verticleLine1 release];
    }
 

    UIImageView *horizonLineUp = [[UIImageView alloc]initWithFrame:CGRectMake(0, 82, 320, 1)];
    UIImage *lineImg = [UIImage imageNamed:@"img_user_headerline"];
    horizonLineUp.image = lineImg;
//    horizonLineUp.backgroundColor = [UIColor colorWithHex:0xffe6e6e6];
    [self.contentView addSubview:horizonLineUp];
    [horizonLineUp release];
    
//    UIView *horizonLineDown = [[UIView alloc]initWithFrame:CGRectMake(0, 124, 320, 1)];
//    horizonLineDown.backgroundColor = [UIColor colorWithHex:0xffe6e6e6];
//    [self.contentView addSubview:horizonLineDown];
//    [horizonLineDown release];
//    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void) bindData:(RCUserProfileModel *)dataModel
{
    if (self.isMySelf) {
//        _userMailBox.hidden = NO;
        _userFollow.hidden = YES;
        _indicator.hidden = NO;
        _editBtn.hidden = NO;
    }else{
        _editBtn.hidden = YES;
        _indicator.hidden = YES;

//        _userMailBox.hidden = YES;
        [_userMailBox setBackgroundImage:[UIImage imageNamed:@"bg_follow_btn"] forState:UIControlStateNormal];
        _userMailBox.frame = CGRectMake(180, 50, 40, 25);
        [_userMailBox setTitle:@"发私信" forState:UIControlStateNormal];
        [_userMailBox setTitleColor:[UIColor colorWithHex:0xff6e6e6e] forState:UIControlStateNormal];
        _userMailBox.titleLabel.font = [UIFont fontWithName:kFontNameHelvetica size:10];

        _userFollow.hidden = NO;
        [_userFollow setTitleColor:[UIColor colorWithHex:0xff6e6e6e] forState:UIControlStateNormal];
        _userFollow.titleLabel.font = [UIFont fontWithName:kFontNameHelvetica size:10];
        [_userFollow setBackgroundImage:[UIImage imageNamed:@"bg_follow_btn"] forState:UIControlStateNormal];

        if (dataModel.hasFocused) {
            [_userFollow setTitle:@"取消关注" forState:UIControlStateNormal];
        }else{
            [_userFollow setTitle:@"关注" forState:UIControlStateNormal];
        }
    }
    
    
    
    {
    UIImage *img = [UIImage imageNamed:@"icon_RCUserVC_avata"];
    [_userAvatar setImageWithURL:[NSURL URLWithString:dataModel.userAvata] placeholderImage:img];
    CGRect frame = _userAvatar.frame;
    frame.size.height = img.size.height, frame.size.width = img.size.width;
    _userAvatar.frame = frame;
    }
    
    _userNameLabel.text = dataModel.userName;
    [_userNameLabel sizeToFit];
    
    {
        UIImage *img = nil;
        if (dataModel.isMan) {
            img = [UIImage imageNamed:@"icon_RCUserVC_male"];;
        }else{
            img = [UIImage imageNamed:@"icon_RCUserVC_female"];;
        }
        
        [_userSex setImageWithURL:nil placeholderImage:img];
        CGRect frame = _userSex.frame;
        frame.origin.x = _userNameLabel.frame.origin.x + _userNameLabel.frame.size.width + 10;
        frame.size.height = img.size.height, frame.size.width = img.size.width;
        _userSex.frame = frame;
    }
    

    
    for (int i = 0; i <3 ; i++) {
        UILabel *labelNumber = (UILabel *)[self viewWithTag:(201+i)];
        if (i == 0) {
            labelNumber.text = [NSString stringWithFormat:@"%d",dataModel.userCheckInCount];
        }else if (i == 1){
            labelNumber.text = [NSString stringWithFormat:@"%d",dataModel.userCommentCount];
        }else{
            labelNumber.text = [NSString stringWithFormat:@"%d",dataModel.userPhotoCount];
        }
//        labelNumber.text = @"123";
    }
   
    if ([NSString stringIsNullOrEmpty:dataModel.liveCity]) {
        _userPlace.hidden = YES;
    }else{
        _userPlace.hidden = YES;//展示隐藏
        [_userPlace setTitle:[NSString stringWithFormat:@"常居地：%@",dataModel.liveCity]forState:UIControlStateNormal];
    }
}


-(void) sectionBtnClick:(id)sender
{
    FansListVCType  listType;
    NSInteger tagNumber = [(UIButton *)sender tag];
    if (tagNumber == 301) {
        listType = FansListVCType_myCheckIn;
    }else if (tagNumber == 302){
        listType = FansListVCType_dianping;
    }else if (tagNumber == 303){
        listType = FansListVCType_photo;
    }else if (tagNumber == 304){
        listType = FansListVCType_mailBox;
    }else if (tagNumber == 305){
        [_delegate followOrCancelSomebody];
        return;
    }else if (tagNumber == 306){
        [_delegate editMyProfile];
        return;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(btnClickToListVC:)]) {
        [_delegate btnClickToListVC:listType];
    }
    
}

@end


@implementation RCUserInteractCell{
    UIImageView *_leftIcon;
    UILabel     *_content;
}
@synthesize isLast;

- (id) init
{
    self = [super init];
    if (self) {
        [self createCellPlaceHolder];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void) createCellPlaceHolder
{
    _leftIcon = [[UIImageView alloc] initWithFrame:CGRectMake(28, 16, 0, 0)];
    [self.contentView addSubview:_leftIcon];
    [_leftIcon release];
    
    _content = [[UILabel alloc] initWithFrame:CGRectMake(60, 18, 120, 15)];
    [_content setBackgroundColor:[UIColor clearColor]];
    _content.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    _content.textColor = kNormalTextColor;
    [self.contentView addSubview:_content];
    [_content release];
    
    
    for (int i = 0; i < 2; i++) {
        UIView *verticleLine1 = [[UIView alloc]initWithFrame:CGRectMake(16+i*(320-32), 0, 1, 46)];
        verticleLine1.backgroundColor = [UIColor colorWithHex:0xffeaeaea];
        [self.contentView addSubview:verticleLine1];
        [verticleLine1 release];
        
        UIView *horizonLine = [[UIView alloc]initWithFrame:CGRectMake(17, i*45, 320-34, 1)];
        horizonLine.tag = 200 + i;
        horizonLine.backgroundColor = [UIColor colorWithHex:0xffe6e6e6];
        [self.contentView addSubview:horizonLine];
        [horizonLine release];
        
    }
    

    

}


- (void) bindData:(RCUserInteractType)type 
{
    UIImage *img = nil;

    switch (type) {
        case RCUserInteractType_Order:
            img = [UIImage imageNamed:@"icon_RCUserVC_Btnorder"];

            _content.text = @"订单";
            break;
        case RCUserInteractType_Collect:
            img = [UIImage imageNamed:@"icon_RCUserVC_Btncollect"];

            _content.text = @"收藏";
            break;
        case RCUserInteractType_Score:
            img = [UIImage imageNamed:@"icon_RCUserVC_Btnscore"];

            _content.text = @"积分";
            break;
        case RCUserInteractType_Message:
            _content.text = @"留言";
            img = [UIImage imageNamed:@"icon_RCUserVC_Btnmessage"];

            break;
        case RCUserInteractType_Follow:
            _content.text = @"关注";
            img = [UIImage imageNamed:@"icon_RCUserVC_Btnfollow"];

            break;
        case RCUserInteractType_Fans:
            img = [UIImage imageNamed:@"icon_RCUserVC_Btnfans"];
            _content.text = @"粉丝";

            break;
        default:
            break;
    }
    
    [_leftIcon autoResizeToSetImage:img];
    
    
    if (self.isLast) {
        [self viewWithTag:201].hidden = NO;
    }else{
        [self viewWithTag:201].hidden = YES;
    }
}
@end
